

//测距的图层
let layer = null;
//测距控件
let control = null;

/**
 * 初始化测距控件
 */
export function init(map, control_select) {
  control = new SuperMap.Control.Measure(
    SuperMap.Handler.Path, { persist: true, immediate: true }
  );
  control.events.on({
    "measure": handleMeasure
  });
  control.style = { fillColor: "#CC3333", strokeColor: "#CC3333", pointRadius: 6 };
  map.addControl(control);

  layer = new SuperMap.Layer.Vector("layer_measureFeatures");
  map.addLayer(layer, 2);

  let layers = control_select.layers;
  layers.push(layer);
  control_select.setLayer(layers);
}

/**
 * 激活控件
 */
export function activate() {
  control.activate();
}

/*
 * 当前测距矢量元素的索引
 */
let index = 0;
/**
 * 测距完成
 */
function handleMeasure(e) {
  control.deactivate();

  index++;

  var style = {
    strokeColor: "#CC3333",
    strokeOpacity: 1,
    strokeWidth: 3,
    pointRadius: 6
  }
  var geometry = e.geometry;
  var f = new SuperMap.Feature.Vector(geometry, null, style);
  f.attributes = {
    id: "measure-line-" + index
  }
  layer.addFeatures(f);

  var start, end;

  var length_componets = geometry.components.length;
  var distance1 = 0;
  for (var k = 0; k < length_componets; k++) {
    var comp = geometry.components[k];
    var point = new SuperMap.Geometry.Point(comp.x, comp.y);

    var pointFeature = new SuperMap.Feature.Vector(point, {
      id: "measure-line-" + index
    });

    pointFeature.style = {
      fillColor: "#fffff",
      strokeColor: "#FF0000",
      pointRadius: 5,
      strokeOpacity: 0.5,
      fillOpacity: 0.5
    };
    layer.addFeatures(pointFeature);

    if (k == 0) {
      start = point;
    }
    else if (k == (length_componets - 1)) {
      end = point;
    }
  }

  var units;
  if (e.units == "km") {
    units = "公里";
  }
  else {
    units = "米";
  }
  
  var distance = e.measure.toFixed(3);
  var distanceinfo = "总长：<font color=red>" + distance + "</font>" + units;

  var startFeature = new SuperMap.Feature.Vector(
    new SuperMap.Geometry.Point(start.x, start.y),
    {
      type: "measure",
      id: "measure-line-" + index
    },
    {
      externalGraphic:"src/css/imgs/measure/begin.gif",
      graphicWidth: 34,
      graphicHeight: 19,
      graphicXOffset: -17,
      graphicYOffset: -25
    }
  );

  var endFeature = new SuperMap.Feature.Vector(
    new SuperMap.Geometry.Point(end.x, end.y),
    {
      type: "clear-measure",
      id: "measure-line-" + index
    },
    {
      externalGraphic: "src/css/imgs/measure/close1.gif",
      graphicWidth: 12,
      graphicHeight: 12,
      graphicXOffset: -15,
      graphicYOffset: -15,
      graphicTitle: "清除本次测量结果",
      cursor: "pointer"
    }
  );

  var lblFeature = new SuperMap.Feature.Vector(
    new SuperMap.Geometry.Point(end.x, end.y),
    {
      type: "measure",
      id: "measure-line-" + index
    },
    {
      externalGraphic: "src/css/imgs/measure/back.png",
      graphicWidth: 106,
      graphicHeight: 20,
      graphicXOffset: 6,
      graphicYOffset: -10,
      cursor: "pointer",
      label: "总长：" + distance + units,
      fontColor: "black",
      fontFamily: "microsoft yahei",
      fontWeight: "normal",
      labelXOffset: 56
    }
  );

  layer.addFeatures([startFeature, endFeature, lblFeature]);
}

/**
 * 清空一条测距结果
 */
export function clearMeasure(e) {
  let attr = e.attributes;
	let fs = layer.features, rfs=[];
	for(let i = fs.length; i--; ) {
		let f = fs[i];
		if(f.attributes.id === attr.id) {			
			rfs.push(f);
		}
	}
	layer.removeFeatures(rfs);
}

/**
 * 清空所有测距结果
 */
export function clear() {
  layer.removeAllFeatures();
	index = 0;
}